package io.plus.server;

import io.plus.common.Constants;
import io.plus.common.io;
import io.plus.engine.server.ServerConfiguration;
import io.plus.engine.utils.lock_capable_task.DefaultThreadFactory;
import io.plus.engine.utils.lock_capable_task.LockCapableTaskThreadPoolExecutor;
import io.plus.engine.utils.lock_capable_task.Threads;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.LinkedBlockingQueue;

@Data
@Slf4j
public class ServerConfigurationImpl extends ServerConfiguration {

    private static int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2;
    protected LockCapableTaskThreadPoolExecutor lockCapableTaskThreadPoolExecutor = null;


    public ServerConfigurationImpl(UserConfiguration userConfiguration, ServerHandlerImpl serverHandler) {
        super(serverHandler);

        this.setHeartbeatTimeout(userConfiguration.getHeartbeatTimeout());

        if (this.lockCapableTaskThreadPoolExecutor == null) {
            LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
            String timThreadName = Constants.io;
            this.lockCapableTaskThreadPoolExecutor = new LockCapableTaskThreadPoolExecutor(//
                    CORE_POOL_SIZE,//
                    CORE_POOL_SIZE,//
                    Threads.KEEP_ALIVE_TIME,//
                    queue,//
                    DefaultThreadFactory.getInstance(timThreadName, Thread.NORM_PRIORITY), timThreadName);
            this.lockCapableTaskThreadPoolExecutor.prestartAllCoreThreads();
        }
        userConfiguration.setConfiguration(this);


        io.userConfiguration = userConfiguration;
    }


}
